import org.molamil.acto.core.Logger;
import org.molamil.acto.pages.MovieBuilder;
import org.molamil.acto.pages.PageContext;
import org.molamil.acto.pages.PageRequest;
import org.molamil.acto.pages.master.AbstractMovieMaster;
import org.molamil.acto.pages.master.ExternalPageMaster;
import org.molamil.acto.pages.master.InternalPageMaster;
import org.molamil.acto.pages.master.LinkedPageMaster;
import org.molamil.acto.pages.master.PageMaster;

/**
 * Creates a <code>PageMaster</code> from a <code>PageContext</code> object.
 *
 * @author Jorge Hernandez
 */
class org.molamil.acto.pages.PageBuilder extends MovieBuilder {
	
	
	// Properties
	
	private static var log:Logger = Logger.getLogger("PageBuilder");
	
	
	// Functions
	
	/**
	 * Creates a <code>PageMaster</code> from a <code>PageContext</code> object.
	 * 
	 * @param context <code>PageContext</code> object holding information to create the new <code>PageMaster</code>.<br/>
	 * 				  The only required field for is <code>context.name</code><br/>
	 * 				  If no <code>context.type</code> is set, <code>"external"</code> is used.<br/>
	 * 				  If no <code>context.className</code> is set, <code>"org.molamil.acto.mc.mcw.Thin"</code> is used.<br/>
	 * 				  If no the value of <code>context.className</code> is not a fully qualified class name, the classes will 
	 * 				  be looked up in the <code>org.molamil.acto.mc.mcw</code> package.
	 * @throws InvalidMcwError
	 */
	public static function create(context:PageContext, request:PageRequest):PageMaster {
		log.info("create() name = " + context.name);
		var master:AbstractMovieMaster = AbstractMovieMaster(buildMaster(context.type));
		master.mcWrapper = buildMcWrapper(context.className);
		master.context = context;		master.request = request;
		return PageMaster(master);
	}
	
	private static function buildMaster(type:String):PageMaster {
		log.debug("buildMaster() type = " + type);
		var master:PageMaster;	
		switch (type) {
			case TYPE_LINKED:
				log.debug("the page is linked");
				master = new LinkedPageMaster();
				break;
			case TYPE_INTERNAL:
				log.debug("the page is internal");
				master = new InternalPageMaster();
				break;
			case TYPE_EXTERNAL:
				log.debug("the page is external");
				master = new ExternalPageMaster();
				break;
			default:
				log.info("no type found, external to be used");
				master = new ExternalPageMaster();
		}
		return master;
	}
	

}